package com.Xht.www.servlet;

//后端响应封禁商家的请求

import com.Xht.www.dao.MerchantInfoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ManageMerchantStatusServlet")
public class ManageMerchantStatusServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            int merchantId = Integer.parseInt(request.getParameter("merchantId"));
            String action = request.getParameter("action");

            MerchantInfoDAO dao = new MerchantInfoDAO();
            boolean success = false;
            String message = "";

            if ("ban".equals(action)) {
                // 封禁商家 -> 设置为"黑商"
                success = dao.updateBusinessStatus(merchantId, "黑商");
                message = success ? "封禁成功" : "封禁失败";
            } else if ("unban".equals(action)) {
                // 解封商家 -> 设置为"正常"
                success = dao.updateBusinessStatus(merchantId, "正常");
                message = success ? "解除封禁成功" : "解除封禁失败";
            } else {
                message = "无效操作";
            }

            out.println("{\"success\":" + success + ",\"message\":\"" + message + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            out.println("{\"success\":false,\"message\":\"操作异常，请重试\"}");
        }
    }
}